import cp from 'child_process'
import {resolve} from 'path'
const scriptPath = resolve(__dirname, './index.js')
Promise.resolve().then(() => {
  const child = cp.fork(scriptPath, [])
  let hasInvoke = false

  child.on('error', err => {
    if (hasInvoke) return
    hasInvoke = true
    console.error(err)
  })
  child.on('exit', code => {
    if (hasInvoke) return
    hasInvoke = true
    let err = code === 0 ? null : new Error('exit code: ' + code)
    !err ? '' : console.error(err)
  })
  child.on('message', data => {
    if (hasInvoke) return
    hasInvoke = true
    let result = data.result
    console.log(result)
    console.log('crawler end now...')
  })
})